package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
//        查看购物车表是否有这个商品
        ShoppingCart list = shoppingCartMapper.list(shoppingCart);

        if (list != null) {
//        如果有则进行number+1之后进行更新
            shoppingCart.setNumber(list.getNumber() + 1);
            shoppingCart.setId(list.getId());
//            BigDecimal bigDecimal = new BigDecimal(shoppingCart.getNumber());
//            判断更改的数据是套餐还是菜品
//            菜品
            if (shoppingCart.getDishId() != null) {
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setAmount(dish.getPrice());
            }else {
//             套餐
                Setmeal setmeal = Setmeal.builder()
                        .id(shoppingCart.getSetmealId())
                        .build();
                Setmeal selectById = setmealMapper.selectById(setmeal);
                shoppingCart.setAmount(selectById.getPrice());
            }
//            更新至购物车
            shoppingCartMapper.update(shoppingCart);
        }else {
//         如果没有则进行添加
//         判断添加的是套餐还是菜品
//         如果添加的是套餐
            if (shoppingCart.getSetmealId() != null) {
                Setmeal setmeal = new Setmeal();
                setmeal.setId(shoppingCart.getSetmealId());
                Setmeal selectById = setmealMapper.selectById(setmeal);

                BeanUtils.copyProperties(selectById, shoppingCart);
                shoppingCart.setSetmealId(shoppingCart.getSetmealId());
                shoppingCart.setAmount(selectById.getPrice());
            }else {
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                BeanUtils.copyProperties(dish, shoppingCart);
                shoppingCart.setDishId(shoppingCart.getDishId());
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectAll(shoppingCart);
        return shoppingCarts;
    }

    /**
     * 删除购物车其中一个数据
     * @param shoppingCartDTO
     */
    public void throwOutItem(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
//        当商品数量大于1时 进行数量减少
            Integer number = shoppingCartMapper.selectItem(shoppingCart);
            if (number > 1) {
                shoppingCartMapper.updateNumber(shoppingCart);
//        数量为0时 清除该数据
            }else {
                shoppingCartMapper.deleteItem(shoppingCart);
            }
    }

    /**
     * 清空购物车
     */
    public void throwItems() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.deleteItems(shoppingCart);
    }
}
